Skip to content

fix(batch): Correctly handle child_exceptions being None #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented May 16, 2022

Issue number:

Summary

Following code raises a runtime error:

from aws_lambda_powertools.utilities.batch.exceptions import BaseBatchProcessingError

print(BaseBatchProcessingError())  # would also raise an error

Changes

Please provide a summary of what's being changed

Change:

  • Allow for child_exceptions being None in format_exceptions which is used when the error is printed.
diff --git a/aws_lambda_powertools/utilities/batch/exceptions.py b/aws_lambda_powertools/utilities/batch/exceptions.py
index d90c25f1..65afd918 100644
--- a/aws_lambda_powertools/utilities/batch/exceptions.py
+++ b/aws_lambda_powertools/utilities/batch/exceptions.py
@@ -16,7 +16,7 @@ class BaseBatchProcessingError(Exception):

     def format_exceptions(self, parent_exception_str):
         exception_list = [f"{parent_exception_str}\n"]
-        for exception in self.child_exceptions:
+        for exception in self.child_exceptions or []:
             extype, ex, tb = exception
             formatted = "".join(traceback.format_exception(extype, ex, tb))
             exception_list.append(formatted)
diff --git a/tests/functional/test_utilities_batch.py b/tests/functional/test_utilities_batch.py
index a5e1e706..757d2ddc 100644
--- a/tests/functional/test_utilities_batch.py
+++ b/tests/functional/test_utilities_batch.py
@@ -908,3 +908,7 @@ def test_batch_processor_error_when_entire_batch_fails(sqs_event_factory, record

     # THEN raise BatchProcessingError
     assert "All records failed processing. " in str(e.value)
+
+
+def test_child_exceptions_is_none():
+    assert str(SQSBatchProcessingError()) == "\n"

User experience

Please share what the user experience looks like before and after this change

Following code would not raise any errors

from aws_lambda_powertools.utilities.batch.exceptions import BaseBatchProcessingError

err = BaseBatchProcessingError(msg="something")
err.format_exceptions("parent exception string")
print(err)

Other solutions considered

  • Changing the typing of child_exceptions is not possible as msg has a default value.
  • Setting child_exceptions to () would generate a typing error with MyPy
  • Setting child_exceptions to [] would make the default argument mutable
  • Changing both msg and child_exceptions to be required would be a breaking change
  • Setting self. child_exceptions is [] when None would also change the type of child_exceptions

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Change:
- Allow for child_exceptions being none in format_exceptions

fixes aws-powertools#1203
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 16, 2022
@github-actions github-actions bot added the bug Something isn't working label May 16, 2022
@sthulb
Copy link
Contributor

sthulb commented May 16, 2022

I can’t stress this enough @michaelbrewer, In an effort to prevent you from wasting time, you should wait for issues to prioritised, this won't be merged for a long time until we're ready to add new features.

@michaelbrewer
Copy link
Contributor Author

Note that this bug will cause a crash when there are no child exceptions.

@michaelbrewer michaelbrewer changed the title fix(batch): allow child_exceptions to be none fix(batch): allow child_exceptions to be None May 17, 2022
@codecov-commenter
Copy link

codecov-commenter commented May 18, 2022

Codecov Report

Merging #1205 (34715fe) into develop (2302099) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           develop    #1205   +/-   ##
========================================
  Coverage    99.88%   99.88%           
========================================
  Files          119      119           
  Lines         5423     5423           
  Branches       618      618           
========================================
  Hits          5417     5417           
  Misses           2        2           
  Partials         4        4           
Impacted Files Coverage Δ
...ws_lambda_powertools/utilities/batch/exceptions.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2bd8722...34715fe. Read the comment docs.

@michaelbrewer michaelbrewer changed the title fix(batch): allow child_exceptions to be None fix(batch): Correctly handle child_exceptions being None Jun 7, 2022
@sthulb sthulb closed this Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. tests utilities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants